﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Caching;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.Text;
using WebServer.Biz;
using WebServer.DBUtility;
using WebServer.Models;

namespace WebServer
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“News”。
    // 注意: 为了启动 WCF 测试客户端以测试此服务，请在解决方案资源管理器中选择 News.svc 或 News.svc.cs，然后开始调试。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class News : INews
    {
        #region 分类逻辑
        /// <summary>
        /// 获取所有的子分类 
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<NewsCate> ManCate(bool IsDel)
        {
            List<NewsCate> request = new List<NewsCate>();
            request = new NewsBiz().ManCate(IsDel);
            return request;
        }

        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<NewsCate> Cate(int ID, bool IsDel)
        {
            List<NewsCate> request = new List<NewsCate>();
            request = new NewsBiz().Cate(ID, IsDel);
            return request;
        }

        /// <summary>
        /// 获取分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public NewsCate CateByID(int ID)
        {
            NewsCate request = new NewsCate();
            request = new NewsBiz().CateByID(ID);
            return request;
        }

        /// <summary>
        /// 创建分类
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        public bool CateCreate(string Name, int ParentID)
        {
            bool request = false;
            request = new NewsBiz().CateCreate(Name, ParentID);
            return request;
        }

        /// <summary>
        /// 更新分类
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool CateSet(int ID, string Name, int Parent, bool IsDel)
        {
            bool request = false;
            request = new NewsBiz().CateSet(ID, Name, Parent, IsDel);
            return request;
        }


        /// <summary>
        /// 新闻动作
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Fild"></param>
        /// <param name="Fn"></param>
        /// <returns></returns>
        public int CateAction(int ID, string Fild, bool Fn)
        {
            int result = 0;
            result = NewsBiz.CateAction(ID, Fild, Fn);
            return result;
        }

        /// <summary>
        /// 获取判断是否有子节点的分类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<NewsCate> CateList(int ID, bool IsDel)
        {
            List<NewsCate> request = new List<NewsCate>();
            request = NewsBiz.CateList(ID, IsDel);
            return request;
        }
        #endregion

        #region 资讯处理
        /// <summary>
        /// 后台获取新闻
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick">0-不置顶，1-置顶，-1代表全部</param>
        /// <param name="IsHeadline">0-不是头条，1-是头条，-1代表全部</param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel">0-未删除，1-已删除，-1代表全部</param>
        /// <returns></returns>
        public List<News> MNewsList(int CateID, int PageSize, int PageIndex, string Title, string Name, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsBiz().MNewsList(CateID, PageSize, PageIndex, Title, Name, IsStick, IsHeadline, HasImg, Order, Desc, IsDel);
            return request;
        }

        /// <summary>
        /// 后台获取新闻总数
        /// </summary>
        /// <param name="CateID"></param>     
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick">0-不置顶，1-置顶，-1代表全部</param>
        /// <param name="IsHeadline">0-不是头条，1-是头条，-1代表全部</param>
        /// <param name="HasImg"></param>    
        /// <param name="IsDel">0-未删除，1-已删除，-1代表全部</param>
        /// <returns></returns>
        public int MNewsCount(int CateID, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int IsDel)
        {
            int result = 0;
            result = new NewsBiz().MNewsCount(CateID, Title, BourseName, IsStick, IsHeadline, HasImg, IsDel);
            return result;
        }


        /// <summary>
        /// 获取存在交易所
        /// </summary>
        /// <returns></returns>
        public List<NewsBourseGroupEntity> BourseGroup()
        {
            List<NewsBourseGroupEntity> request = new List<NewsBourseGroupEntity>();
            ObjectCache cache = MemoryCache.Default;
            request = cache["BourseGroup"] as List<NewsBourseGroupEntity>;
            if (request == null)
            {
                request = new List<NewsBourseGroupEntity>();
                request = new NewsBiz().BourseGroup();
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now.AddDays(1);
                cache.Set("BourseGroup", request, policy);
            }
            return request;
        }


        /// <summary>
        /// 获取存在交易所【有centerID】
        /// </summary>
        /// <returns></returns>
        public List<NewsBourseGroupEntity> PBourseGroup()
        {
            List<NewsBourseGroupEntity> request = new List<NewsBourseGroupEntity>();
            ObjectCache cache = MemoryCache.Default;
            request = cache["PBourseGroup"] as List<NewsBourseGroupEntity>;
            if (request == null)
            {
                request = new List<NewsBourseGroupEntity>();
                request = new NewsBiz().PBourseGroup();
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now.AddDays(1);
                cache.Set("PBourseGroup", request, policy);
            }
            return request;
        }

        /// <summary>
        /// 根据交易所获取分类的存在分组
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        public List<NewsCate> CateGroup(int Parent, string Name)
        {
            List<NewsCate> request = new List<NewsCate>();
            ObjectCache cache = MemoryCache.Default;
            request = cache["CateGroup_" + Parent.ToString() + "_" + Name] as List<NewsCate>;
            if (request == null)
            {
                request = new List<NewsCate>();
                request = new NewsBiz().CateGroup(Parent, Name);
                CacheItemPolicy policy = new CacheItemPolicy();
                policy.AbsoluteExpiration = DateTime.Now.AddDays(1);
                cache.Set("CateGroup_" + Parent.ToString() + "_" + Name, request, policy);
            }
            return request;
        }

        /// <summary>
        /// 获取新闻详情
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public News NewsByID(int ID)
        {
            News request = new News();
            request = new NewsBiz().NewsByID(ID);
            if (!string.IsNullOrEmpty(request.Text))
            {
                if (request.Text != "")
                {
                    request.Text = System.Web.HttpUtility.HtmlDecode(request.Text);
                }
            }
            return request;
        }

        /// <summary>
        /// 新闻排行获取-不包含评论
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="Size"></param>
        /// <param name="Sort">0:点击量;1:ID;;2创建时间;3:更新时间 ;4:评论</param>
        /// <param name="Field"></param>
        /// <returns></returns>
        public List<News> NewsBySort(int CateID, bool IsHeadle, int Size, int Sort, bool Desc)
        {
            List<News> request = new List<News>();
            request = new NewsBiz().NewsBySort(CateID, IsHeadle, Size, Sort, Desc);
            return request;
        }

        /// <summary>
        /// 根据CenterID获取新闻
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="CenterID"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> NewsListByCenterID(int CateID, int PageSize, int PageIndex, string Title, int CenterID, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsBiz().NewsListByCenterID(CateID, PageSize, PageIndex, Title, CenterID, IsStick, IsHeadline, HasImg, Order, Desc, IsDel);
            return request;
        }

        /// <summary>
        /// 新闻列表
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="Page"></param>
        /// <param name="PageSize"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsHEadLine"></param>
        /// <param name="HasImg"></param>
        /// <returns></returns>
        public List<News> NewsList(int CateID, int Page, int PageSize, string BourseName, bool IsStick, bool IsHeadline, bool HasImg, bool IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsBiz().NewsList(CateID, Page, PageSize, BourseName, IsStick, IsHeadline, HasImg, IsDel);
            return request;
        }


        /// <summary>
        /// 获取没有解读的新闻
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public List<News> NoReadingNewsList(int CateID, int Page, int PageSize, string BourseName, bool IsStick, bool IsHeadline, bool HasImg, bool IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsBiz().NoReadingNewsList(CateID, Page, PageSize, BourseName, IsStick, IsHeadline, HasImg, IsDel);
            return request;
        }


        /// <summary>
        /// 新闻动作
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool NewsAction(int ID, string Filed, bool Fn)
        {
            bool request = false;

            if (!string.IsNullOrEmpty(Filed))
            {
                if (Filed == "IsHeadline" || Filed == "Stick" || Filed == "Commend" || Filed == "IsDel")
                {
                    request = new NewsBiz().NewsAction(ID, Filed, Fn);
                }
            }
            return request;
        }


        /// <summary>
        /// 新增以及更新新闻
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool NewsSet(News N)
        {
            bool request = false;
            if (!string.IsNullOrEmpty(N.Text)) {
                if (N.Text!="")
                {
                    N.Text = System.Web.HttpUtility.HtmlEncode(N.Text);
                }
            }
            request = new NewsBiz().NewsSet(N);
            return request;
        }


        /// <summary>
        /// 新增以及更新新闻
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int NewsInfoSet(News N)
        {
            int result = 0;
            result = new NewsBiz().NewsInfoSet(N);
            return result;
        }

        /// <summary>
        /// 获取新闻详情
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public News NewsInfoByID(int ID)
        {
            News request = new News();
            request = new NewsBiz().NewsInfoByID(ID);
            if (!string.IsNullOrEmpty(request.Text))
            {
                if (request.Text != "")
                {
                    request.Text = System.Web.HttpUtility.HtmlDecode(request.Text);
                }
            }
            return request;
        }

        /// <summary>
        /// 获取上一篇，下一篇
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public List<NewsKeys> NewsCon(int ID)
        {
            List<NewsKeys> request = new List<NewsKeys>();
            request = new NewsBiz().NewsCon(ID);
            return request;
        }
        #endregion

        #region 标签
        /// <summary>
        /// 获取标签
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<NewsKeys> NewsLabel(int PageSize, int PageIndex, int ID, string Name, bool IsDel)
        {
            List<NewsKeys> request = new List<NewsKeys>();
            request = new NewsBiz().NewsLabel(PageSize, PageIndex, ID, Name, IsDel);
            return request;
        }


        /// <summary>
        /// 标签总数
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Name"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public int NewsLabelCount(int ID, string Name, bool IsDel)
        {
            int result = 0;
            result = new NewsBiz().NewsLabelCount(ID, Name, IsDel);
            return result;

        }

        /// <summary>
        /// 根据ID获取标签
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public NewsKeys NewsLabelByID(int ID)
        {
            NewsKeys entity = new NewsKeys();
            entity = new NewsBiz().NewsLabelByID(ID);
            return entity;
        }


        /// <summary>
        /// 添加或者修改标签
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        public MsgEntity NewsLabelSet(NewsKeys N)
        {
            MsgEntity M = new MsgEntity();
            M = new NewsBiz().NewsLabelSet(N);
            return M;
        }


        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Del">真删，还是假删</param>
        /// <returns></returns>
        public int NewsLabelDel(int ID, bool Del)
        {
            int result = 0;
            result = new NewsBiz().NewsLabelDel(ID, Del);
            return result;
        }

        /// <summary>
        /// 新闻关联标签
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        public int NewsConKeySet(NewsConKeys N)
        {

            int result = 0;
            result = new NewsBiz().NewsConKeySet(N);
            return result;
        }
        #endregion

        #region 首页/咨询幻灯片
        /// <summary>
        /// 幻灯片
        /// </summary>
        /// <returns></returns>
        public List<Slide> getImg(int IsOpen, int AppOrWeb)
        {
            List<Slide> result = new List<Slide>();
            result = NewsBiz.getImg(IsOpen, AppOrWeb);
            return result;
        }

        /// <summary>
        /// 后台查询所有幻灯片
        /// </summary>
        /// <returns></returns>
        public List<Slide> getMImg(int IsOpen, int AppOrWeb, int Order, bool Desc)
        {
            List<Slide> result = new List<Slide>();
            result = NewsBiz.getMImg(IsOpen, AppOrWeb, Order, Desc);
            return result;
        }



        /// <summary>
        /// 删除幻灯片
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public int DelImg(int ID)
        {
            int result = 0;
            result = NewsBiz.DelImg(ID);
            return result;
        }

        /// <summary>
        /// 新增或者修改幻灯片
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddOrUpImg(Slide model)
        {
            int result = 0;
            result = NewsBiz.AddOrUpImg(model);
            return result;
        }


        /// <summary>
        /// 根据ID获取幻灯片
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public Slide ImgByID(int ID)
        {
            Slide entity = new Slide();
            entity = NewsBiz.ImgByID(ID);
            return entity;
        }
        #endregion


        #region 解读
        /// <summary>
        /// 后台获取新闻
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> ReadingList(int CateID, int PageSize, int PageIndex, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel, int UserID)
        {
            List<News> request = new List<News>();
            request = new NewsBiz().ReadingList(CateID, PageSize, PageIndex, Title, BourseName, IsStick, IsHeadline, HasImg, Order, Desc, IsDel, UserID);
            return request;
        }


        /// <summary>
        /// 手机查询解读
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> PReadingList(int CateID, int PageSize, string OpenTime, string EndTime, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel, int UserID)
        {
            List<News> request = new List<News>();
            request = new NewsBiz().PReadingList(CateID, PageSize, OpenTime, EndTime, Title, BourseName, IsStick, IsHeadline, HasImg, Order, Desc, IsDel, UserID);
            return request;
        }


        /// <summary>
        /// 总数
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="IsDel"></param>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int ReadingCount(int CateID, string Title, string BourseName, int IsStick, int IsHeadline, bool HasImg, int IsDel, int UserID)
        {
            int result = 0;
            result = new NewsBiz().ReadingCount(CateID, Title, BourseName, IsStick, IsHeadline, HasImg, IsDel, UserID);
            return result;
        }

        /// <summary>
        /// 经济商发表解读
        /// </summary>
        /// <param name="N"></param>
        /// <returns></returns>
        public bool ReadingSet(News N)
        {

            bool request = false;
            if (!string.IsNullOrEmpty(N.Text))
            {
                if (N.Text != "")
                {
                    string Text =  DBUtility.Common.ClearHtml(N.Text);
                    N.SmallTitle = Text.Length > 102 ? Text.Substring(0, 100) : Text;
                }
            }

            if (!string.IsNullOrEmpty(N.Text))
            {
                if (N.Text != "")
                {
                    N.Text = System.Web.HttpUtility.HtmlEncode(N.Text);
                }
            }
            request = new NewsBiz().ReadingSet(N);
            return request;

        }

        /// <summary>
        /// 解读删除
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public MsgEntity ReadingDel(int ID, int UserID)
        {
            MsgEntity request = new MsgEntity();
            request = new NewsBiz().ReadingDel(ID, UserID);
            return request;
        }

        /// <summary>
        /// 解读删除
        /// </summary>
        /// <param name="U"></param>
        /// <returns></returns>
        public int DelReading(int ID, string Admin)
        {
            int result = 0;
            result = new NewsBiz().DelReading(ID, Admin);
            return result;
        }
        #endregion


        #region 手机资讯
        /// <summary>
        /// 后台获取新闻
        /// </summary>
        /// <param name="CateID"></param>
        /// <param name="PageSize"></param>
        /// <param name="PageIndex"></param>
        /// <param name="Title"></param>
        /// <param name="BourseName"></param>
        /// <param name="IsStick"></param>
        /// <param name="IsHeadline"></param>
        /// <param name="HasImg"></param>
        /// <param name="Order">默认为创建时间，1-标题，2-点击率3-交易所，4-回复数</param>
        /// <param name="Desc"></param>
        /// <param name="IsDel"></param>
        /// <returns></returns>
        public List<News> PNewsList(string OpenTime, string EndTime, int PageSize, int CateID, string Title, string Shortened, int IsStick, int IsHeadline, bool HasImg, int Order, bool Desc, int IsDel)
        {
            List<News> request = new List<News>();
            request = new NewsBiz().PNewsList(OpenTime, EndTime, PageSize, CateID, Title, Shortened, IsStick, IsHeadline, HasImg, Order, Desc, IsDel);
            return request;
        }
        #endregion

    }
}
